﻿# --------------------------- #
# Visual Studio Configuration #
# --------------------------- #
param([string]$virtualMachineService, [string]$virtualMachineName)

Write-Information "Virtual machine '$virtualMachineName' executing Visual Studio '$($script.type)' configuration ... " -NoNewLine $true
$userName = $subscription.virtualMachines.adminUserName
$password = $subscription.virtualMachines.password
$netBiosDomain = $subscription.virtualMachines.netBiosDomain
$uri = Get-AzureWinRMUri -ServiceName $virtualMachineService -Name $virtualMachineName
$credential = New-Object System.Management.Automation.PSCredential(($netBiosDomain + "\" + $userName), (ConvertTo-SecureString $password -AsPlainText -Force))
$session = New-PSSession -ComputerName $uri[0].DnsSafeHost -Credential $credential -Port (Get-VirtualMachineInstanceEndpointPort $subscription $virtualMachineName) -UseSSL
switch ($script.type)
{
	"Install"
	{
		Send-RemoteFile -Source "$env:dp0\Files\$($script.configuration)" -Destination "C:\VS.xml" -Session $session
		Invoke-Command -Session $session -ScriptBlock {
            if ((Start-Process -FilePath "V:\vs_ultimate.exe" -ArgumentList "/AdminFile C:\VS.xml /Quiet /NoRestart /NoWeb /Log Visual_Studio_Install.log" -PassThru -Wait).ExitCode -ne 0)
            {
                throw "Visual Studio installation failed. Check '$Env:USERPROFILE\Documents\Visual_Studio_Install.log' for more details."
            }
			Remove-Item "C:\VS.xml" -Force
		}
	}
	default
	{
		throw "Visual Studio type '$($script.type)' is not supported."
	}
}
Remove-PSSession $session
Write-Text "done."